package com.changgou.goods.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.changgou.common.entity.HttpRespond;
import com.changgou.common.enums.HttpStateEnum;
import com.changgou.common.exception.ChangGouException;
import com.changgou.goods.dao.SpecMapper;
import com.changgou.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper specMapper;


    @Override
    public HttpRespond<List<Map>> findListByCategoryName(String categoryName) {
        List<Map> listByCategoryName = this.specMapper.findListByCategoryName(categoryName);
        if(CollectionUtil.isEmpty(listByCategoryName)){
            throw new ChangGouException(HttpStateEnum.NOT_FOUND);
        }
        listByCategoryName = listByCategoryName.stream().map(spc -> {
            String[] options = String.valueOf(spc.get("options")).split(",");
            spc.put("options", options);
            return spc;
        }).collect(Collectors.toList());

        return new HttpRespond<>(HttpStateEnum.SUCCESS, listByCategoryName);
    }
}
